//
//  GeneralParameterManager+DeviceModel.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "GeneralParameterManager+DeviceModel.h"

//
#import <sys/utsname.h>

@implementation GeneralParameterManager (DeviceModel)

// See https://www.theiphonewiki.com/wiki/Models for identifiers
- (NSString *)currentDeviceModel {
#if TARGET_OS_SIMULATOR
    return @"Simulator";
#else
//    if (IS_SIMULATOR) {
//        return @"simulator";
//    }
    //    NSString *deviceIdentifier = [QMUIHelper deviceModel];
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceIdentifier = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //
    if ([deviceIdentifier rangeOfString:@"iPhone"].location!=NSNotFound) {//iPhone系列设备
        return [self iPhoneNameFromIdentifier:deviceIdentifier];
    }
    if ([deviceIdentifier rangeOfString:@"iPad"].location!=NSNotFound) {//iPad系列设备
        return [self iPadNameFromIdentifier:deviceIdentifier];
    }
    if ([deviceIdentifier rangeOfString:@"iPod"].location!=NSNotFound) {//iPod系列设备
        return [self iPodTouchNameFromIdentifier:deviceIdentifier];
    }
    //Apple Watch \ Apple TV  \ AirPods 
    return @"Unknown Device";
#endif
}
/**
 根据identifier返回iPhone系列设备名称
 */
- (NSString *)iPhoneNameFromIdentifier:(NSString *)identifier {
    if ([identifier isEqualToString:@"iPhone1,1"]) {
        return @"iPhone";
    }else if ([identifier isEqualToString:@"iPhone1,2"]) {
        return @"iPhone 3G";
    }else if ([identifier isEqualToString:@"iPhone2,1"]) {
        return @"iPhone 3GS";
    }else if ([identifier isEqualToString:@"iPhone3,1"]||
              [identifier isEqualToString:@"iPhone3,2"]||
              [identifier isEqualToString:@"iPhone3,3"]) {
        return @"iPhone 4";
    }else if ([identifier isEqualToString:@"iPhone4,1"]) {
        return @"iPhone 4S";
    }else if ([identifier isEqualToString:@"iPhone5,1"]||
              [identifier isEqualToString:@"iPhone5,2"]) {
        return @"iPhone 5";
    }else if ([identifier isEqualToString:@"iPhone5,3"]||
              [identifier isEqualToString:@"iPhone5,4"]) {
        return @"iPhone 5c";
    }else if ([identifier isEqualToString:@"iPhone6,1"]||
              [identifier isEqualToString:@"iPhone6,2"]) {
        return @"iPhone 5s";
    }else if ([identifier isEqualToString:@"iPhone7,2"]) {
        return @"iPhone 6";
    }else if ([identifier isEqualToString:@"iPhone7,1"]) {
        return @"iPhone 6 Plus";
    }else if ([identifier isEqualToString:@"iPhone8,1"]) {
        return @"iPhone 6s";
    }else if ([identifier isEqualToString:@"iPhone8,2"]) {
        return @"iPhone 6s Plus";
    }else if ([identifier isEqualToString:@"iPhone8,4"]) {
        return @"iPhone SE";
    }else if ([identifier isEqualToString:@"iPhone9,1"]||
              [identifier isEqualToString:@"iPhone9,3"]) {
        return @"iPhone 7";
    }else if ([identifier isEqualToString:@"iPhone9,2"]||
              [identifier isEqualToString:@"iPhone9,4"]) {
        return @"iPhone 7 Plus";
    }else if ([identifier isEqualToString:@"iPhone10,1"]||
              [identifier isEqualToString:@"iPhone10,4"]) {
        return @"iPhone 8";
    }else if ([identifier isEqualToString:@"iPhone10,2"]||
              [identifier isEqualToString:@"iPhone10,5"]) {
        return @"iPhone 8 Plus";
    }else if ([identifier isEqualToString:@"iPhone10,3"]||
              [identifier isEqualToString:@"iPhone10,6"]) {
        return @"iPhone X";
    }else if ([identifier isEqualToString:@"iPhone11,8"]) {
        return @"iPhone XR";
    }else if ([identifier isEqualToString:@"iPhone11,2"]) {
        return @"iPhone XS";
    }else if ([identifier isEqualToString:@"iPhone11,6"]||
              [identifier isEqualToString:@"iPhone11,4"]) {
        return @"iPhone XS Max";
    }
    return @"iPhone New Device";
}
/**
 根据identifier返回iPad系列设备名称
 */
- (NSString *)iPadNameFromIdentifier:(NSString *)identifier {
    if ([identifier isEqualToString:@"iPad1,1"]) {
        return @"iPad";
    }else if ([identifier isEqualToString:@"iPad2,1"]||
              [identifier isEqualToString:@"iPad2,2"]||
              [identifier isEqualToString:@"iPad2,3"]||
              [identifier isEqualToString:@"iPad2,4"]) {
        return @"iPad 2";
    }else if ([identifier isEqualToString:@"iPad3,1"]||
              [identifier isEqualToString:@"iPad3,2"]||
              [identifier isEqualToString:@"iPad3,3"]) {
        return @"iPad (3rd generation)";
    }else if ([identifier isEqualToString:@"iPad3,4"]||
              [identifier isEqualToString:@"iPad3,5"]||
              [identifier isEqualToString:@"iPad3,6"]) {
        return @"iPad (4th generation)";
    }else if ([identifier isEqualToString:@"iPad4,1"]||
              [identifier isEqualToString:@"iPad4,2"]||
              [identifier isEqualToString:@"iPad4,3"]) {
        return @"iPad Air";
    }else if ([identifier isEqualToString:@"iPad5,3"]||
              [identifier isEqualToString:@"iPad5,4"]) {
        return @"iPad Air 2";
    }else if ([identifier isEqualToString:@"iPad6,7"]||
              [identifier isEqualToString:@"iPad6,8"]) {
        return @"iPad Pro (12.9-inch)";
    }else if ([identifier isEqualToString:@"iPad6,3"]||
              [identifier isEqualToString:@"iPad6,4"]) {
        return @"iPad Pro (9.7-inch)";
    }else if ([identifier isEqualToString:@"iPad6,11"]||
              [identifier isEqualToString:@"iPad6,12"]) {
        return @"iPad (5th generation)";
    }else if ([identifier isEqualToString:@"iPad7,1"]||
              [identifier isEqualToString:@"iPad7,2"]) {
        return @"iPad Pro (12.9-inch) (2nd generation)";
    }else if ([identifier isEqualToString:@"iPad7,3"]||
              [identifier isEqualToString:@"iPad7,4"]) {
        return @"iPad Pro (10.5-inch)";
    }else if ([identifier isEqualToString:@"iPad7,5"]||
              [identifier isEqualToString:@"iPad7,6"]) {
        return @"iPad (6th generation)";
    }else if ([identifier isEqualToString:@"iPad8,1"]||
              [identifier isEqualToString:@"iPad8,2"]||
              [identifier isEqualToString:@"iPad8,3"]||
              [identifier isEqualToString:@"iPad8,4"]) {
        return @"iPad Pro (11-inch)";
    }else if ([identifier isEqualToString:@"iPad8,5"]||
              [identifier isEqualToString:@"iPad8,6"]||
              [identifier isEqualToString:@"iPad8,7"]||
              [identifier isEqualToString:@"iPad8,8"]) {
        return @"iPad Pro (12.9-inch) (3rd generation)";
    }
    
    //iPad Mini
    if ([identifier isEqualToString:@"iPad2,5"]||
        [identifier isEqualToString:@"iPad2,6"]||
        [identifier isEqualToString:@"iPad2,7"]) {
        return @"iPad mini";
    }else if ([identifier isEqualToString:@"iPad4,4"]||
              [identifier isEqualToString:@"iPad4,5"]||
              [identifier isEqualToString:@"iPad4,6"]) {
        return @"iPad mini 2";
    }else if ([identifier isEqualToString:@"iPad4,7"]||
              [identifier isEqualToString:@"iPad4,8"]||
              [identifier isEqualToString:@"iPad4,9"]) {
        return @"iPad mini 3";
    }else if ([identifier isEqualToString:@"iPad5,1"]||
              [identifier isEqualToString:@"iPad5,2"]) {
        return @"iPad mini 4";
    }
    
    return @"iPad New Device";
}
/**
 根据identifier返回iPod Touch系列设备名称
 */
- (NSString *)iPodTouchNameFromIdentifier:(NSString *)identifier {
    if ([identifier isEqualToString:@"iPod1,1"]) {
        return @"iPod touch";
    }else if ([identifier isEqualToString:@"iPod2,1"]) {
        return @"iPod touch (2nd generation)";
    }else if ([identifier isEqualToString:@"iPod3,1"]) {
        return @"iPod touch (3rd generation)";
    }else if ([identifier isEqualToString:@"iPod4,1"]) {
        return @"iPod touch (4th generation)";
    }else if ([identifier isEqualToString:@"iPod5,1"]) {
        return @"iPod touch (5th generation)";
    }else if ([identifier isEqualToString:@"iPod7,1"]) {
        return @"iPod touch (6th generation)";
    }
    return @"iPod Touch New Device";
}

@end
